import { queryProduct, addProduct, changeProductState, queryProductDetails, changeProductDetails } from '../services/api';
import { add } from 'gl-matrix/src/gl-matrix/vec2';
import { message } from 'antd';

export default {
  namespace: 'product',

  state: {
    productList: [],
    page: [],
    productDiviList: {},
    channelList: [],
    retailsList: [],
    loading: false,
    confirmLoading: false,
    productDetails: {},
  },

  effects: {

    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      });

      const response = yield call(queryProduct, payload);
      // console.log("ALALALALALLALALALLA")
      // console.log(JSON.stringify(response))
      yield put({
        type: 'save',
        payload: {
          productList: response.data.list,
          page: response.data.page,
          loading: false,
        },
      });
      if (callback) callback();
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'confirmLoading',
        payload: true,
      });
      const response = yield call(addProduct, payload);
      yield put({
        type: 'confirmLoading',
        payload: false,
      });
      if(response.result.code == 200){
        message.success("添加成功")
      }else{
        message.error(response.result.message)
      }
      
      yield put({
        type: 'save',
        data: response
      });
      if (callback) callback(response.result.code);
    },
    //修改产品状态
    *changeProductState({ payload, callback }, { call, put }) {
      const response = yield call(changeProductState, payload);
      if (response.result.code == "200") {
        message.success('改变状态成功');
      } else {
        message.error(response.result.message);
      }
      yield put({
        type: 'save',
        payload: { data: response },
      });
      if (callback) callback();

    },

    //显示产品详情
    *queryProductDetails({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      });
      const response = yield call(queryProductDetails, payload);
      // console.log("ADAASASASASASASA")
      // console.log(response)
      yield put({
        type: 'save',
        payload: {
          productDetails: response.data.product,
          channelList: response.data.channelList,
          retailsList: response.data.retailList,
          loading: false,
        },
      });
      if (callback) callback();
    },

    //修改产品详情
    *changeProductDetails({ payload, callback }, { call, put }) {
      // console.log("payload",payload)
      yield put({
        type: 'confirmLoading',
        payload: true,
      });
      const response = yield call(changeProductDetails, payload);
      // console.log("response",response)
      yield put({
        type: 'confirmLoading',
        payload: false,
      });
      if(response.result.code == 200){
        message.success("提交成功")
      }else{
        message.error(response.result.message)
      }
      
      yield put({
        type: 'save',
        payload: {
          data: response
        },
      });
      if (callback) callback(response.result.code);
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    confirmLoading(state, { payload }) {
      return {
        ...state,
        confirmLoading: payload,
      };
    },
   
  },
};
